package com.zpb.framework.utils;

import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: zhaopengbo
 * @Date: 2025/9/9
 */
public class BeanCopyUtil {
    private BeanCopyUtil(){

    }
    // 普通对象拷贝
    public static <V> V copyBean(Object source, Class<V> targetClass) {
        if (source == null) {
            return null;
        }
        V target;
        try {
            Constructor<V> declaredConstructor = targetClass.getDeclaredConstructor();
            target = declaredConstructor.newInstance();
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        BeanUtils.copyProperties(source, target);
        return target;
    }
    //集合对象拷贝
    public static <T,V> List<V> copyBeanList(List<T> sourceList, Class<V> targetClass) {
        List<V> targetList = sourceList.stream()
                .map(source -> BeanCopyUtil.copyBean(source, targetClass))
                .collect(Collectors.toList());
        return targetList;
    }


}
